package com.isoft.controller.restful;

import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.isoft.IService.ISysUserService;
import com.isoft.common.helper.ServiceManager;
import com.isoft.domain.SysUser;

@RestController
@RequestMapping(value="/sysUser")
public class SysUserController {

	private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);
	
	/*@Autowired
	private ISysUserService sysUserService;*/

	@RequestMapping(value = "/findSysUserById/{id}")
    public SysUser findSysUserById(@PathVariable Integer id){
        return ServiceManager.sysUserService.findBySysUserId(id);
    }
	
	@RequestMapping(value = "/insertOne")
    public void insertOne(@Valid SysUser user,BindingResult bindingResult){
		if(bindingResult.hasErrors()) {
    		System.out.println(bindingResult.getFieldError().getDefaultMessage());
    		return;
    	}
		ServiceManager.sysUserService.insertOne(user);
    }
	
	@RequiresRoles("ADMIN")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(Model model) {
		String userName = (String) SecurityUtils.getSubject().getPrincipal();
		model.addAttribute("username", userName);
		return "index";
	}

	@RequestMapping("/pages/403")
	public String unauthorizedRole() {
		logger.info("------没有权限-------");
		return "pages/403";
	}
}
